package com.levi.listeners;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.levi.common.Constants;
import com.levi.common.SpringUtils;
import com.levi.domain.Approval;
import com.levi.service.ApprovalService;
import org.flowable.engine.TaskService;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.delegate.TaskListener;
import org.springframework.stereotype.Component;

/**
 * @author shentong
 * @date 2021/8/16 11:50
 * @Description 任务监听器，用于设置一级审批人
 */
@Component
public class MyTaskListener implements TaskListener{

    private ApprovalService approvalService;

    private TaskService taskService;

    public MyTaskListener() {
        this.approvalService = SpringUtils.getBean(ApprovalService.class);
        this.taskService = SpringUtils.getBean(TaskService.class);
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        LambdaQueryWrapper<Approval> qw = new LambdaQueryWrapper<>();
        qw.eq(Approval::getType, Constants.BUSINESS_TYPE_LEAVE);
        Approval approval = approvalService.getOne(qw);
        if ("一级审批".equals(delegateTask.getName())
        && "create".equals(delegateTask.getEventName())) {
            //设置一级审批人
            taskService.setAssignee(delegateTask.getId(),approval.getUserOne());
        }else if("二级审批".equals(delegateTask.getName())
                && "create".equals(delegateTask.getEventName())){
            //设置二级审批人
            taskService.setAssignee(delegateTask.getId(),approval.getUserTwo());
        }
    }
}
